/*
 * Project: BoboDesignPattern
 * 
 * File Created at 2019年6月8日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package test.lesson3.tree.binarytree.sorttree;

/**
 * @Type Node
 * @Desc
 * @author 52489
 * @date 2019年6月8日
 * @Version V1.0
 */
public class Node {

    int value;

    Node left;

    Node right;

    /**
     * @param value
     */
    public Node(int value) {
        this.value = value;
    }

    public void add(Node node) {
        if (node == null) {
            return;
        }

        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }

        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }

    }

    /**
     * @param node
     */
    public void midleShow(Node node) {
        if (null == node) {
            return;
        }
        midleShow(node.left);
        System.out.println(node.value);
        midleShow(node.right);
    }

    /**
     * @param value2
     * @return
     */
    public Node search(int value) {
        if (this.value == value) {
            return this;
        } else if (value < this.value) {
            if (left == null) {
                return null;
            }
            return left.search(value);
        } else {
            if (right == null) {
                return null;
            }
            return right.search(value);
        }
    }

    /**
     * @param value2
     * @return
     */
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (this.value > value && this.left != null) {
                return this.left.searchParent(value);

            } else if (this.value < value && this.right != null) {
                return this.right.searchParent(value);

            }
            return null;
        }
    }
}
